package com.whmnx.chitchat.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;


import com.whmnx.chitchat.base.BaseApplication;
import com.whmnx.chitchat.constants.Constants;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;


/**
 * 封装首选项
 */
public class SPUtil {
    /**
     * 获取SharedPreference
     */
    private static SharedPreferences getSharedPreferece(Context context) {
        SharedPreferences sp = context.getSharedPreferences(Constants.PFNAME, Context.MODE_PRIVATE);
        return sp;
    }


    /**
     * 创建一个解决SharedPreferencesCompat.apply方法的一个兼容类
     *
     * @author Charming
     */
    private static class SharedPreferencesCompat {
        private static final Method sApplyMethod = findApplyMethod();

        /**
         * 反射查找apply的方法
         */
        @SuppressWarnings({"unchecked", "rawtypes"})
        private static Method findApplyMethod() {
            try {
                Class clz = Editor.class;
                return clz.getMethod("apply");
            } catch (NoSuchMethodException e) {
            }

            return null;
        }

        /**
         * 如果找到则使用apply执行，否则使用commit
         */
        public static void apply(Editor editor) {
            try {
                if (sApplyMethod != null) {
                    sApplyMethod.invoke(editor);
                    return;
                }
            } catch (IllegalArgumentException e) {
            } catch (IllegalAccessException e) {
            } catch (InvocationTargetException e) {
            }
            editor.commit();
        }
    }
    /**
     * 获取String值
     */
    public static String getString(String key) {
        SharedPreferences sp = getSharedPreferece(BaseApplication.getContext());
        String result = sp.getString(key, null);
        return result;
    }




    /**
     * 获取boolean值
     */
    public static boolean getBoolean(String key) {
        SharedPreferences sp = getSharedPreferece(BaseApplication.getContext());
        boolean result = sp.getBoolean(key, false);
        return result;
    }

    /**
     * 获取int值
     */
    public static int getInt(String key, int i) {
        SharedPreferences sp = getSharedPreferece(BaseApplication.getContext());
        int result = sp.getInt(key, 0);
        return result;
    }

    /**
     * 向首选项存取数据(仅限于String,Boolean,Integer)
     */
    public static void put(String key, Object value) {
        SharedPreferences sp = getSharedPreferece(BaseApplication.getContext());
        Editor editor = sp.edit();
        if (value instanceof String) {
            editor.putString(key, (String) value);
        } else if (value instanceof Integer) {
            editor.putInt(key, (Integer) value);
        } else if (value instanceof Boolean) {
            editor.putBoolean(key, (Boolean) value);
        }
        SharedPreferencesCompat.apply(editor);
    }


    public static void saveDocPhoto(Context context, String userId, Object object) {
        SharedPreferences sp = context.getSharedPreferences(Constants.PFNAME, Context.MODE_PRIVATE);
        Editor editor = sp.edit();
        editor.putString(userId + "Photo", (String) object);
        SharedPreferencesCompat.apply(editor);
    }


    //主业
    public static String getDocPhoto(Context context, String userId) {
        SharedPreferences sp = context.getSharedPreferences(Constants.PFNAME, Context.MODE_PRIVATE);
        return sp.getString(userId + "Photo", "");
    }




    /**
     * 清除所有数据
     */
    public static void clear(Context context) {
        SharedPreferences sp = getSharedPreferece(BaseApplication.getContext());
        Editor editor = sp.edit();
        editor.clear();
        SharedPreferencesCompat.apply(editor);
    }
}
